﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using DRHotel_v1.Classes;
using DRHotel_v1.Classes.DBManagers;

namespace DRHotel_v1.RoomAvaAndBooking
{
    public partial class CustomerUpdateFrm : Form
    {
        public string cusIC;
        public string staffID;
        public CustomerUpdateFrm()
        {
            InitializeComponent();
        }

        private void CustomerInfoFrm_Load(object sender, EventArgs e)
        {
            Customer cus = RoomAvaAndBookingDBManager.GetCusByIC(cusIC);
            tbxICType.Text = cus.ICType.ToString();
            tbxICNo.Text = cus.cusIC.ToString();
            tbxFirstName.Text = cus.firstName.ToString();
            tbxLastName.Text = cus.lastName.ToString();
            if (cus.gender)
            {
                rbnMale.Checked = true;
            }
            else
            {
                rbnFemale.Checked = true;
            }
            cbbCustomerType.Text = cus.cusType;
            tbxCountry.Text = cus.country.ToString();
            dateTimePicker1.Value = cus.dob;
            tbxPhone.Text = cus.phoneNo.ToString();
            tbxEmail.Text = cus.email.ToString();
            tbxAddress.Text = cus.mailingAddress.ToString();
            tbxPostal.Text = cus.postCode.ToString();

        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!IsValidEmailAddress(tbxEmail.Text))//validate email address
            {
                MessageBox.Show("Invalid Email Address!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxEmail.SelectAll();
                tbxEmail.Focus();
                return;
            }
            //create customer class and fill info
            Customer cus = new Customer();
            cus.cusIC = tbxICNo.Text;
            cus.ICType = tbxICType.Text;
            cus.firstName = tbxFirstName.Text;
            cus.lastName = tbxLastName.Text;
            if (rbnMale.Checked == true)
            {
                cus.gender = true;
            }
            else
            {
                cus.gender = false;
            }
            cus.dob = dateTimePicker1.Value;
            cus.country = tbxCountry.Text;
            cus.phoneNo = tbxPhone.Text;
            cus.email = tbxEmail.Text;
            cus.mailingAddress = tbxAddress.Text;
            cus.postCode = tbxPostal.Text;
            cus.cusType = cbbCustomerType.Text;

            //create customer modification class and fill info
            CustomerModification cm = new CustomerModification();
            MainForm frm = new MainForm();

            cm.staffID = Convert.ToInt32(staffID);
            cm.cusIC = cusIC;
            cm.dateTime = DateTime.Now;

            bool success2 = HousekeepingDBManager.InsertCustomerModification(cm);

            //update info into database
            if (RoomAvaAndBookingDBManager.UpdateCustomerInfo(cus) != 0)
            {
                MessageBox.Show("Customer information updated successful!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                MessageBox.Show("Cannot update customer information!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

        }

        #region Input validation
        public bool IsValidEmailAddress(string email)//validate email address
        {
            try
            {
                MailAddress ma = new MailAddress(email);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void tbxPhone_TextChanged(object sender, EventArgs e)//validate phone numbers
        {
            Regex regex = new Regex("^[0-9]+$");
            if (regex.IsMatch(tbxPhone.Text))
            {
                errorProvider1.SetError(tbxPhone, String.Empty);
            }
            else
            {
                errorProvider1.SetError(tbxPhone, "Only numbers may be entered here");
            }
        }

        private void tbxPostal_TextChanged(object sender, EventArgs e)//validate Postal code (Alphabetical letters is allowed for some countries)
        {
            Regex regex = new Regex("^[A-Z0-9]*$");
            if (regex.IsMatch(tbxPostal.Text))
            {
                errorProvider1.SetError(tbxPostal, String.Empty);
            }
            else
            {
                errorProvider1.SetError(tbxPostal, "Only capital letters and numbers may be entered here");
            }
        }

        private void tbxEmail_TextChanged(object sender, EventArgs e)
        {
            if (IsValidEmailAddress(tbxEmail.Text))
            {
                errorProvider1.SetError(tbxEmail, String.Empty);
            }
            else
            {
                errorProvider1.SetError(tbxEmail, "Only capital letters and numbers may be entered here");
            }
        }
        #endregion
    }
}
